package com.jic.market.impl.rest.discount.processor;

import com.jic.market.common.util.Money;
import com.jic.market.entity.PromotionLimitRuleEntity;
import com.jic.market.entity.PromotionRuleEntity;
import com.jic.market.enums.DiscountAmountServiceOperateEnum;
import com.jic.market.impl.rest.discount.model.GoodsDetailModel;
import com.jic.market.mapper.PromotionLimitRuleEntityMapper;
import com.jic.market.mapper.PromotionRuleEntityMapper;
import com.jic.market.request.rest.discount.DiscountBaseRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 第X件Y折
 * @author yinhaijun
 * @date: 2020/5/13
 */
@Service("XofferDiXyZeProcessor")
public class XofferDiXyZeProcessor extends AbstractSingleDiscountProcessor {

    @Autowired
    private PromotionRuleEntityMapper promotionRuleEntityMapper;
    @Autowired
    private PromotionLimitRuleEntityMapper promotionLimitRuleEntityMapper;

    @Override
    public SingleResult isCondition(GoodsDetailModel goodsDetailModel, DiscountBaseRequest request, DiscountAmountServiceOperateEnum operate) {
        if(!checkPromotion(request.getThirdUserId(), request.getAdminOperate().getChildId().toString(), goodsDetailModel.getChannelCodes(), goodsDetailModel.getPromotionMemberGradeList())){
            return new SingleResult(false, "第X件Y折计算"+goodsDetailModel.getPromotionCode()+"会员等级校验未通过");
        }
        PromotionLimitRuleEntity promotionLimitRuleEntity = promotionLimitRuleEntityMapper.getEntityByPromotionIdAndMpId(Long.parseLong(goodsDetailModel.getPromotionId()), goodsDetailModel.getSkuId());
        LimitRuleResult limitRuleResult = checkCommonLimitRule(promotionLimitRuleEntity, goodsDetailModel.getPromotionId(), goodsDetailModel, goodsDetailModel.getFrontPromotionTypeEnum(), request.getThirdUserId());
        if(!limitRuleResult.getLimitCheck()){
            return new SingleResult(false, "第X件Y折促销"+goodsDetailModel.getPromotionCode()+"商品"+goodsDetailModel.getThirdSkuId()+"超出活动购买限制");
        }

        // 超出购买按原价处理
        List<PromotionRuleEntity> promotionRuleEntityList = promotionRuleEntityMapper.queryByPromotionId(Long.parseLong(goodsDetailModel.getPromotionId()));
        // 首先获取阶梯等级最高的数据
        BigDecimal contentValue = getFlightContentValue(goodsDetailModel.getOverlayAppFlag(), goodsDetailModel.getOverlayAppNum(), new BigDecimal(goodsDetailModel.getNum()), promotionRuleEntityList, 2);

        // 不优惠处理
        if(null == contentValue || contentValue.compareTo(BigDecimal.ZERO) == 0){
            return new SingleResult(false, "第X件Y折无优惠");
        }
        SingleResult SingleResult = new SingleResult(true, "有优惠");
        SingleResult.setObject(contentValue);
        return SingleResult;
    }

    @Override
    public void queryShoppingCartAfter(SingleResult singleResult, GoodsDetailModel goodsDetailModel, DiscountBaseRequest request) {
        querySettlementAfter(singleResult, goodsDetailModel, request);
    }

    @Override
    public void querySettlementAfter(SingleResult singleResult, GoodsDetailModel goodsDetailModel, DiscountBaseRequest request){
        // 优惠总价 = （1-折扣/10） *原价*数量
        BigDecimal zhekou1 = Money.subtract(new BigDecimal(1), Money.divide((BigDecimal) singleResult.getObject(), new BigDecimal(10)));
        BigDecimal promotionDiscountTotalAmount =  Money.multiply(Money.multiply(zhekou1, goodsDetailModel.getSalePrice()), new BigDecimal(goodsDetailModel.getSingleValidNum()));
        goodsDetailModel.setPromotionDiscountTotalAmount(promotionDiscountTotalAmount);
    }

}
